﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ADB_QuanLyThuVien
{
    public partial class frmPhieuMuonSach : DevExpress.XtraEditors.XtraForm
    {
        private Account acc;
        List<Book> lBook = new List<Book>();
        Member mem = new Member();
        List<Member> lMember;
        private List<Book> lBookRequest;
        private Member reader;
        private frmTrangChu frmTrangChu;
        public frmPhieuMuonSach()
        {
            InitializeComponent();
        }

        public frmPhieuMuonSach(Account acc)
        {
            this.acc = acc;
            InitializeComponent();
        }
        public frmPhieuMuonSach(Account acc, List<Book> lbook, Member dg)
        {
            this.acc = acc;
            this.lBook = lbook;
            this.mem = dg;
            InitializeComponent();
        }

        public frmPhieuMuonSach(Account acc, List<Book> lbook, Member dg, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.acc = acc;
            this.lBook = lbook;
            this.mem = dg;
            InitializeComponent();
            this.frmTrangChu = frmTrangChu;
        }

        public frmPhieuMuonSach(Account acc, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.acc = acc;
            this.frmTrangChu = frmTrangChu;
            InitializeComponent();
        }
        private void frmPhieuMuonSach_Load(object sender, EventArgs e)
        {
            rdb_TaiTV.Checked = true;
            pTaiTV.Visible = true;
            pOnline.Visible = false;
            List<Member> lReader = DAO_Member.SelectAllReader();
            //foreach (var item in lReader)
            //{
            //    cbbDocGia.Properties.Items.Add(item.Name);
            //}
            lBookDataGridView.DataSource = new List<Book>();
            lBookDataGridView.DataSource = lBook;
            QLTV_System.SetNumberColumn(lBookDataGridView);
            if (mem.Email != null)
            {
                lReaderId.Text = mem.Id;
                lReaderName.Text = mem.Name;
                lReaderEmail.Text = mem.Email;
                lReaderPhoneNumber.Text = mem.PhoneNumber;
            }
        }

        private void FillFormReader(string nameReader)
        {
            mem = DAO_Member.SelectMemberByName(nameReader);
            lReaderEmail.Text = mem.Email;
            lReaderPhoneNumber.Text = mem.PhoneNumber;
        }

        private void rdb_TaiTV_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb_TaiTV.Checked == true)
            {
                pTaiTV.Visible = true;
                pOnline.Visible = false;
                lBookDataGridView.DataSource = new List<Book>();
                btnAdd.Visible = true;
                btnRemove.Visible = true;
            }
            else
            {
                List<RequestForm> lRequest = new List<RequestForm>();
                lRequest = DAO_RequestForm.SelectAll();
                requestFormBindingSource1.DataSource = lRequest;
                pTaiTV.Visible = false;
                pOnline.Visible = true;
                btnAdd.Visible = false;
                btnRemove.Visible = false;
            }
        }

        private void cbbMaPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string idForm = cbbMaPhieu.SelectedText;
            //RequestForm rq = DAO_RequestForm.SelectRFByID(idForm);
            //emailTextBox1.Text = rq.Reader.Email;
            //nameTextBox.Text = rq.Reader.Name;
            //dateCreateFormTextBox.Text = rq.DateCreateForm.ToString("dd-MM-yyyy");
            //lBookDataGridView.DataSource = new List<Book>();
            //lBookDataGridView.DataSource = rq.LBook;
            //QLTV_System.SetNumberColumn(lBookDataGridView);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string tabTittle = "Tra cứu sách"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            for (int i = 0; i < frmTrangChu.xtraTabControl1.TabPages.Count; i++)
            {
                if (frmTrangChu.xtraTabControl1.TabPages[i].Text == tabTittle)
                {
                    frmTrangChu.xtraTabControl1.TabPages.Remove(frmTrangChu.xtraTabControl1.TabPages[i]);
                    break;
                }
            }
            frmTrangChu.xtraTabControl1.TabPages.Add(tab);
            var hien = new frmTraCuuSach(lBook, acc, mem, frmTrangChu);
            hien.TopLevel = false;
            hien.Dock = DockStyle.Fill;
            tab.Controls.Add(hien);
            hien.Show();
            frmTrangChu.xtraTabControl1.SelectedTabPageIndex = frmTrangChu.xtraTabControl1.TabPages.Count - 1;
            //var frm = new frmTraCuuSach(lBook, acc, mem);
            //frm.Show();
            //this.Hide();
        }
        private void pictureEdit3_Click(object sender, EventArgs e)
        {
            pSeacrhDocGia.Visible = true;
        }

        private void pictureEdit5_Click(object sender, EventArgs e)
        {
            if (rId.Checked == true)
                lMember = DAO_Member.SelectMemberByID(txtSearchDocGia.Text);
            else
                if (rName.Checked == true)
                    lMember = DAO_Member.SelecMemberContainName(txtSearchDocGia.Text);
                else
                    if (rEmail.Checked == true)
                        lMember = DAO_Member.SelectMemberContainEmail(txtSearchDocGia.Text);
                    else
                        lMember = DAO_Member.SelectMemberContainPhoneNumber(txtSearchDocGia.Text);
            memberDataGridView.DataSource = new List<Member>();
            memberDataGridView.DataSource = lMember;
            QLTV_System.SetNumberColumn(memberDataGridView);
        }

        private void pbtnSelectReader_Click(object sender, EventArgs e)
        {
            try
            {
                mem = lMember[memberDataGridView.CurrentRow.Index];
                pSeacrhDocGia.Visible = false;
                if (rdb_TaiTV.Checked == true)
                {
                    lReaderId.Text = mem.Id;
                    lReaderName.Text = mem.Name;
                    lReaderEmail.Text = mem.Email;
                    lReaderPhoneNumber.Text = mem.PhoneNumber;
                }
                else
                {
                    lOReaderName.Text = mem.Name;
                    lOReaderEmail.Text = mem.Email;
                    lOReaderPhone.Text = mem.PhoneNumber;
                }
            }
            catch
            {
                MessageBox.Show("Bạn chưa chọn độc giả... không thể tiến hành tiếp được...");
            }
        }

        private void btnSearchReaderOnline_Click(object sender, EventArgs e)
        {
            pSeacrhDocGia.Visible = true;
        }

        private void btnAddPhieuMuon_Click(object sender, EventArgs e)
        {
            if (lBook.Count > 3)
            {
                MessageBox.Show("Số lượng sách bạn sách mượn vượt quá giới hạn...");
            }
            else
            {
                if (rdb_TaiTV.Checked == true)
                {
                    PhieuMuon pm = new PhieuMuon();
                    pm.DateCreateForm = DateTime.Today;
                    pm.ExpirationDate = pm.DateCreateForm.AddDays(14);
                    pm.rqf = null;
                    //MessageBox.Show(pm.ExpirationDate.ToString("dd-MM-yyyy"));
                    pm.LBook = new List<Book>();
                    string kq = DAO_PhieuMuon.AddPhieuMuon(pm, lBook, acc, lReaderEmail.Text);
                    MessageBox.Show(kq);
                }
                else
                {
                    PhieuMuon pm = new PhieuMuon();
                    pm.DateCreateForm = DateTime.Today;
                    pm.ExpirationDate = pm.DateCreateForm.AddDays(14);
                    pm.LBook = new List<Book>();
                    string kq = DAO_PhieuMuon.AddBFFRF(pm, lBook, acc, lOReaderEmail.Text);
                    MessageBox.Show(kq);
                }
                frmPhieuMuonSach_Load(sender, e);
            }
        }

        private void pictureEdit1_Click(object sender, EventArgs e)
        {
            RequestForm rq = DAO_RequestForm.SelectRFByEmail(lOReaderEmail.Text);
            lBookDataGridView.DataSource = new List<Book>();
            List<Book> lBookNotBorrow = new List<Book>();
            for (int i = 0; i<rq.lBookRequest.Count;++i)
                if(rq.lBookRequest[i].IsBorrow == false)
                    lBookNotBorrow.Add(rq.lBookRequest[i].bookRequest);
            lBookDataGridView.DataSource = lBookNotBorrow;
            QLTV_System.SetNumberColumn(lBookDataGridView);
        }

        private void lOReaderEmail_TextChanged(object sender, EventArgs e)
        {
            lBook = DAO_RequestForm.SelectBookByEmail(lOReaderEmail.Text);
            lBookDataGridView.DataSource = lBook;
            QLTV_System.SetNumberColumn(lBookDataGridView);
        }
    }
}